package cn.edu.cdcas.service;

import cn.edu.cdcas.entity.Answer;
import cn.edu.cdcas.entity.Student;
import cn.edu.cdcas.mapper.AnswerMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service("answerService")
public class AnswerService {

    @Resource
    private AnswerMapper answerMapper;

    @Resource
    private StudentService studentService;

    public Answer getById(Integer id) {
        return answerMapper.getById(id);
    }

    public void update(Answer answer) {
        answerMapper.update(answer);
    }

    public void complete(Student student) {
        int pass = student.getPass();
        if (student.getExamState() == 1) {
            int totalScore = answerMapper.totalScore(student, 1);
            student.setExamScore(totalScore);
            if (totalScore >= pass) {
                student.setExamState(2);
                student.setQualified(2);
            } else {
                student.setExamState(3);
                student.setQualified(1);
            }
            student.setExamEnd(new Date());
        } else {
            int totalScore = answerMapper.totalScore(student, 2);
            student.setMakeUpScore(totalScore);
            if (totalScore >= pass) {
                student.setMakeUpState(2);
                student.setQualified(2);
            } else {
                student.setMakeUpState(3);
                student.setQualified(3);
            }
            student.setMakeUpEnd(new Date());
        }
        studentService.update(student);
    }

}
